<?php

namespace app\business\controller;

use app\business\model\CustomerNatureModel;
use cmf\controller\AdminBaseController;

class CustomerNatureController extends AdminBaseController
{
    /**
     * 客户性质列表
     * @adminMenu(
     *     'name'   => '客户性质列表',
     *     'parent' => 'business/CustomerNatureList/index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '客户性质列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $type = $this->request->param('type', 0, 'intval');
        if (!$type) {
            $this->error('参数非法');
        }

        $customerNatureModel = new CustomerNatureModel();
        $lists = $customerNatureModel->where(['type' => $type])->paginate(10);

        $page = $lists->render();
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        $this->assign('type', $type);

        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $customerNatureModel = new CustomerNatureModel();
            $arrData = $this->request->post();

            $result = $customerNatureModel->allowField(true)->validate(true)->save($arrData);
            if ($result) {
                $this->success(lang("ADD_SUCCESS"), url("index", ['type' => $arrData['type']]));
            } else {
                $this->error($customerNatureModel->getError());
            }
        }
        $type = $this->request->param('type', 0, 'intval');
        if (!$type) {
            $this->error('参数非法');
        }
        $this->assign('type', $type);
        return $this->fetch();
    }

    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        $customerNatureModel = new CustomerNatureModel();
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $result = $customerNatureModel->allowField(true)->validate(true)->save($arrData, ["id" => $arrData["id"]]);
            if ($result !== false) {
                $this->success(lang("EDIT_SUCCESS"), url("index", ['type' => $arrData['type']]));
            } else {
                $this->error($customerNatureModel->getError());
            }
        }
        $intId = $this->request->param("id", 0, 'intval');

        $result = $customerNatureModel->where(["id" => $intId])->find();

        $this->assign('result', $result);
        return $this->fetch();
    }

    public function select()
    {
        $id = $this->request->param('id', 0, 'intval');
        $type = $this->request->param('type', 0, 'intval');
        $customerNatureModel = new CustomerNatureModel();

        $where = ['status' => 1, 'type' => $type];
        $lists = $customerNatureModel->where($where)->order('list_order,id')->select();

        $this->assign('lists', $lists);
        $this->assign('id', $id);
        return $this->fetch();
    }
}